package com.niuxiaofa.newlaw.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.niuxiaofa.newlaw.Bean.DemandTagsBean;
import com.niuxiaofa.newlaw.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class DemandTagsAdapter extends RecyclerView.Adapter<DemandTagsAdapter.ViewHolder> {
    public List<DemandTagsBean.DataDTO> listEntities;
    DemandTagsAdapter.Onclick onclick;

    int postion=0;

    public DemandTagsAdapter(List<DemandTagsBean.DataDTO> newsList, DemandTagsAdapter.Onclick onclick) {
        this.onclick = onclick;
        this.listEntities = newsList;
    }

    
    @Override
    public DemandTagsAdapter.ViewHolder onCreateViewHolder( ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_demand_tag, viewGroup, false);
        DemandTagsAdapter.ViewHolder viewHolder = new DemandTagsAdapter.ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder( DemandTagsAdapter.ViewHolder viewHolder, int i) {

        DemandTagsBean.DataDTO listEntity = listEntities.get(i);

        viewHolder.name.setText(listEntity.getName());

        if (postion==i){
            viewHolder.name.setTextColor(Color.WHITE);
            viewHolder.name.setBackgroundResource(R.drawable.shape_orange_all);
        }else {
            viewHolder.name.setTextColor(Color.parseColor("#FFFC5B0F"));
            viewHolder.name.setBackgroundResource(R.drawable.shape_orange);
        }

        viewHolder.name.setOnClickListener(e -> {
            onclick.onclick(i);
            postion=i;
            notifyDataSetChanged();
        });
    }

    @Override
    public int getItemCount() {
        return listEntities.size();
    }

    public interface Onclick {

        void onclick(int c);

    }

    static class ViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.name)
        TextView name;


        public ViewHolder( View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
